import CfgPointer from "../basic/cfg-pointer";

export default {
  components: {CfgPointer},
  model: { prop:'value', event:'change' },
  data(){
    return {
      modalConfig: {
        transfer: true,
        closable: false,
        maskClosable: false,
        width: 90,
        styles: {top: '50px', maxWidth: '1200px'},
      },
      rowData: {},
      drawer:{
        show:false
      },
    }
  },
  methods:{
    handleDbClick(row, index) {
      this.rowData = row
      this.submit()
    },
    handleSelect(selection, row) {
      this.rowData = row
    },
    handleSelectCancel(selection, row) {
      this.rowData = {}
    },
    select(){
      this.$set(this.drawer,'show',true)
    },
    clear() {
      this.$emit('change', {}, {})
    },
    ok() {
      this.submit()
    },
    submit(){
      let data = this.rowData
      let res = {}
      
      // 选择一条记录后，将 rowData 映射为需要的key，再触发value的change
      Object.keys(this.submitObj).forEach(key => {
        let newKey = this.submitObj[key] || key
        data[newKey] && (res[key] = data[newKey])
      })
      this.defaultObj && Object.keys(this.defaultObj).forEach(key => {
        res[key] = this.defaultObj[key]
      })
      console.log("file: pointerMixin.js:46 submit:", this.defaultObj,this.submitObj,data,res)
      this.$emit('change', res, data)
      this.drawer.show = false
    }
  },
  computed: {
    editValue() {
      let res = {}

      // 初始化时，在已选择的值回显之前，将
      Object.keys(this.submitObj).forEach(key => {
        let newKey = this.submitObj[key] || key
        this.value && this.value[key] && (res[newKey] = this.value[key])
      });
      return res
    },
    lang() {
      return {
        "en-US": {
          type: 'type',
          className: 'class name',
          instanceName: 'instance name',
          category: 'category',
          eventName: 'event name',
          search:'search',
          reset:'reset',
          add:'Add',
          edit:'edit',
          delete:'delete',
          comments:'comments',
          version:'version',
          published:'published',
          createTime:'createTime',
          updateTime:'updateTime'
        },
        "zh-CN": {
          type: '所属类型',
          className: '模型类名',
          instanceName: '实例名',
          category: '分类',
          eventName: '事件名称',
          search:'查询',
          reset:'重置',
          add:'添加',
          edit:'编辑',
          delete:'删除',
          comments:'注释',
          version:'版本',
          published:'已发布',
          createTime:'创建时间',
          updateTime:'更新时间'
        },
      }[this.G_Locale]
    },
  },
  props:{
    // 选择数据后，需要回填的默认值，如 { linkModel: 'consume' }
    defaultObj: null,
    /* 选择模型后，需要返回的数据字段映射map，有下面几个例子: 
      demo: { type: '', className: '' }  // 只需要回显字段: type className
      demo: { linkType: 'type', linkClassName: 'className' } // 读取pointer选择后的type className，然后返回为 linkType linkClassName
    */
    submitObj: {
      default() {
        return {
          type: '',
          className: '',
        }
      }
    },
    type:{
      default:()=>{
        return '';
      }
    },
    prefix:{
      default:()=>{
        return '';
      }
    },
    mark:{
      default:()=>{
        return false;
      }
    },
    label:{
      default:()=>{
        return '';
      }
    },
    value:{
      default:()=>{
        return {};
      }
    },
    tip: {
      default: () => {
        return null;
      }
    },
    addField: {
      default: () => {
        return false;
      }
    },
    removeField: {
      default: () => {
        return false;
      }
    },
    disabled: {
      return: false
    },
    title: {
      default:()=>{
        return '';
      }
    },
  },
}